package cn.micai.base.string;

/**
 * 描述：
 * <p>
 *
 * @author: 赵新国
 * @date: 2018/6/11 15:22
 */
public class LenghtOfStringMain {

    public static void main(String [] args) {

        String helloWorld = "This is hello world";
        System.out.println("length of helloWorld string: " + getLengthOfStringWithCharArray(helloWorld));
        System.out.println("length of helloWorld string: " + helloWorld.length());
        System.out.println("length of helloWorld string: " + getLengthOfString(helloWorld));

    }

    public static int getLengthOfString(String str) {
        int i = 0;
        try {
            for (i=0;;i++) {
                str.charAt(i);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return i;
    }

    public static int getLengthOfStringWithCharArray(String str) {
        int length = 0;
        char[] strCharArray = str.toCharArray();
        for (char c : strCharArray) {
            length++;
        }
        return length;
    }
}
